CC := $(CC)
AR := $(AR)

SUBDIRS := xml-tool 
SUBDIRS += libmdm
SUBDIRS += cms_core
SUBDIRS += cms_db
SUBDIRS += cms_task
SUBDIRS += cmc

CMS_BASE_DIR := $(CURDIR)
CMS_INC_DIR := $(CMS_BASE_DIR)/include/
CMS_LIB_DIR := $(CMS_BASE_DIR)/lib/

CMS_LIBMDM_DIR := $(CMS_BASE_DIR)/libmdm
CMS_XMLTOOL_DIR := $(CMS_BASE_DIR)/xml-tool
CMS_XMLTOSHM_DIR :=$(CMS_XMLTOOL_DIR)/xmlToShm
CMS_TASK_DIR := $(CMS_BASE_DIR)/cms_task

########## Needed MACRO DEFINITION in .c ##############################
DEF_CONF_FILE_XML	:= "config.default.xml"
DEF_CONF_FILE_ENC	:= "config.default.enc"
PRE_CONF_FILE_SHM	:= "config.default.cms"
CUR_CONF_FILE_XML	:= "config.current.xml"
CUR_CONF_FILE_ENC	:= "config.current.enc"


CUR_XML_PATH 		:= "$(CMS_LIB_DIR)"
DEF_XML_PATH 		:= "$(CMS_LIB_DIR)"

CFLAGS += -DCUR_XML_PATH=\"$(CUR_XML_PATH)\"
CFLAGS += -DDEF_XML_PATH=\"$(DEF_XML_PATH)\"

CFLAGS += -DDEF_CONF_FILE_XML=\"$(DEF_CONF_FILE_XML)\"
CFLAGS += -DDEF_CONF_FILE_ENC=\"$(DEF_CONF_FILE_ENC)\"
CFLAGS += -DPRE_CONF_FILE_SHM=\"$(PRE_CONF_FILE_SHM)\"
CFLAGS += -DCUR_CONF_FILE_XML=\"$(CUR_CONF_FILE_XML)\"
CFLAGS += -DCUR_CONF_FILE_ENC=\"$(CUR_CONF_FILE_ENC)\"
#######################################################################


CFLAGS += -DX2S_MEMORY_SIZE=3145728
CFLAGS += -I$(CMS_INC_DIR)

CFLAGS += -g -Wall -O2

LDFLAGS += -L$(CMS_LIB_DIR)


export CFLAGS
export LDFLAGS
export CC
export AR

export CMS_BASE_DIR
export CMS_INC_DIR
export CMS_LIB_DIR
export CMS_LIBMDM_DIR
export CMS_XMLTOOL_DIR
export CMS_XMLTOSHM_DIR
export CMS_TASK_DIR
export DEF_CONF_FILE_XML
export DEF_CONF_FILE_ENC
export PRE_CONF_FILE_SHM
export CUR_CONF_FILE_XML
export CUR_CONF_FILE_ENC



.PHONY: all
all: prepare $(SUBDIRS)
	@echo "=============================================="
	@echo "         SCM cms Build success"
	@echo "=============================================="		


$(SUBDIRS): ECHO 
	make -C $@

ECHO:
	@echo "============================================"
	@echo "subdirs: $(SUBDIRS)"
	@echo "============================================"



.PHONY: clean prepare
prepare:
	@echo "=============================================="
	@echo "         SCM cms Build start"
	@echo "=============================================="		
	@if [ ! -d $(CMS_INC_DIR) ]; then\
		mkdir $(CMS_INC_DIR);\
	fi;
	@if [ ! -d $(CMS_LIB_DIR) ]; then\
		mkdir $(CMS_LIB_DIR);\
	fi;
	@if [ -f $(CMS_XMLTOSHM_DIR)/pdm.c ] ; then \
		cmp -s $(CMS_XMLTOSHM_DIR)/pdm.c $(CMS_LIBMDM_DIR)/pdm.c;\
		if [ $$? -ne 0 ]; then\
			cp $(CMS_LIBMDM_DIR)/pdm.c $(CMS_XMLTOSHM_DIR)/;\
		fi;\
	else\
		cp $(CMS_LIBMDM_DIR)/pdm.c $(CMS_XMLTOSHM_DIR)/;\
	fi;
	cp inc/*.h $(CMS_INC_DIR)


clean:
	@for subdir in $(SUBDIRS);\
	do\
		make -C $${subdir} clean;\
	done;
	rm -rf $(CMS_INC_DIR) $(CMS_LIB_DIR)


